const util = require('../../../utils/util.js');
var user = require('../../../services/user.js');
var app = getApp();
Page({
  data: {
    isLogin: false,
    showKefu: false,
    orderlist: []
  },
  onLoad: function() {
    let token = wx.getStorageSync('token')
    if (token) {
      this.setData({
        isLogin: true
      })
    } else {
      wx.navigateTo({
        url: '../../login/login',
      })
    }
  },
  onShow:function(){
    let token = wx.getStorageSync('token')
    console.log(token)
    if (token) {
      this.setData({
        isLogin: true
      })
      this.getorderlist()
    }
  },
  getorderlist() {
    let orderinfo = wx.getStorageSync('userInfo').id;
    let that = this
    util.request('order/orderlist', 'GET').then(function(res) {
      console.log(res);
      if (res.errno === 0) {
        res.data.forEach(item =>{
          item.pay_amount = parseFloat(item.pay_amount / 100).toFixed(2)
          item.buy_time = that.formatDateTime(item.buy_time)
        })
        that.setData({
          orderlist: res.data
        })
      }
    });
  },
  kefuShow() {
    this.setData({
      showKefu: true
    })
  },
  closeKefu() {
    this.setData({
      showKefu: false
    })
  },
  goClass(e) {
    let id = e.currentTarget.dataset.lessonid;
    let lessontype = e.currentTarget.dataset.lessontype;
    if (lessontype === 1) {
      wx.navigateTo({
        url: '/pages/lessoninfo/lessoninfo?id=' + id + '&lessontype=' + lessontype
      })
    }
    if (lessontype === 2) {
      wx.navigateTo({
        url: '/pages/lessonliveinfo/lessonliveinfo?id=' + id + '&lessontype=' + lessontype
      })
    }
    
  },
  formatDateTime(timeStamp) {
    var date = new Date();
    date.setTime(timeStamp * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  },
  switchIndex() {
    wx.switchTab({
      url: '/pages/index/index'
    })
  }
})